<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>xhrmonitor.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_net_xhrmonitor.js.html">xhrmonitor.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2007 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Class used by XHR wrappers to publish their state to IframeIo
<a name="line17"></a> * or other components that need to know if any XmlHttpRequests are active.
<a name="line18"></a> *
<a name="line19"></a> * IframeIo needs to know if any XmlHttpRequests have been initiated from the
<a name="line20"></a> * result of an incremental data response, so that it can delay the destruction
<a name="line21"></a> * of the iframe.  Otherwise firefox will error since the source context no
<a name="line22"></a> * longer exists.
<a name="line23"></a> *
<a name="line24"></a> * This class means that IframeIo does not have an explicit dependency on
<a name="line25"></a> * XhrIo.
<a name="line26"></a> *
<a name="line27"></a> * See https://bugzilla.mozilla.org/show_bug.cgi?id=369939 for a description of
<a name="line28"></a> * the problem and a minimal test case.
<a name="line29"></a> *
<a name="line30"></a> * This class&#39;s methods are no-ops for non-Gecko browsers.
<a name="line31"></a> *
<a name="line32"></a> */
<a name="line33"></a>
<a name="line34"></a>goog.provide(&#39;goog.net.xhrMonitor&#39;);
<a name="line35"></a>
<a name="line36"></a>goog.require(&#39;goog.array&#39;);
<a name="line37"></a>goog.require(&#39;goog.debug.Logger&#39;);
<a name="line38"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line39"></a>
<a name="line40"></a>
<a name="line41"></a>/**
<a name="line42"></a> * Class used for singleton goog.net.xhrMonitor which can be used for monitoring
<a name="line43"></a> * whether there any XmlHttpRequests have been opened in a given execution
<a name="line44"></a> * context, and allowing query of when they are closed.
<a name="line45"></a> * @constructor
<a name="line46"></a> * @private
<a name="line47"></a> */
<a name="line48"></a>goog.net.XhrMonitor_ = function() {
<a name="line49"></a>  if (!goog.userAgent.GECKO) return;
<a name="line50"></a>
<a name="line51"></a>  /**
<a name="line52"></a>   * A map of context identifiers to an array of XHR hashcodes that were created
<a name="line53"></a>   * in the context.
<a name="line54"></a>   * String -&gt; Array.&lt;String&gt;
<a name="line55"></a>   * @type {Object}
<a name="line56"></a>   * @private
<a name="line57"></a>   */
<a name="line58"></a>  this.contextsToXhr_ = {};
<a name="line59"></a>
<a name="line60"></a>  /**
<a name="line61"></a>   * Inverse lookup from an XHR hashcode to any context that was open when it
<a name="line62"></a>   * was created.  There should rarely be multiple open contexts, but support
<a name="line63"></a>   * has been added for completeness.
<a name="line64"></a>   * String -&gt; Array.&lt;String&gt;
<a name="line65"></a>   * @type {Object}
<a name="line66"></a>   * @private
<a name="line67"></a>   */
<a name="line68"></a>  this.xhrToContexts_ = {};
<a name="line69"></a>
<a name="line70"></a>  /**
<a name="line71"></a>   * Stack of active contexts.
<a name="line72"></a>   * @type {Array.&lt;string&gt;}
<a name="line73"></a>   * @private
<a name="line74"></a>   */
<a name="line75"></a>  this.stack_ = [];
<a name="line76"></a>
<a name="line77"></a>};
<a name="line78"></a>
<a name="line79"></a>
<a name="line80"></a>/**
<a name="line81"></a> * Returns a string key for the argument -- Either the string itself, the
<a name="line82"></a> * hashcode of the object, or an empty string otherwise.
<a name="line83"></a> * @param {Object|string} obj The object to make a key for.
<a name="line84"></a> * @return {string|number} A string key for the argument.
<a name="line85"></a> */
<a name="line86"></a>goog.net.XhrMonitor_.getKey = function(obj) {
<a name="line87"></a>  return goog.isString(obj) ? obj :
<a name="line88"></a>         goog.isObject(obj) ? goog.getHashCode(obj) :
<a name="line89"></a>         &#39;&#39;;
<a name="line90"></a>};
<a name="line91"></a>
<a name="line92"></a>
<a name="line93"></a>/**
<a name="line94"></a> * A reference to the xhrMonitor logger.
<a name="line95"></a> * @type {goog.debug.Logger}
<a name="line96"></a> * @private
<a name="line97"></a> */
<a name="line98"></a>goog.net.XhrMonitor_.prototype.logger_ =
<a name="line99"></a>    goog.debug.Logger.getLogger(&#39;goog.net.xhrMonitor&#39;);
<a name="line100"></a>
<a name="line101"></a>
<a name="line102"></a>/**
<a name="line103"></a> * Flag indicating that the monitor should be used.
<a name="line104"></a> * Should be set to false for worker threads as they do not have access
<a name="line105"></a> * to iframes, which is what the monitor is needed for.
<a name="line106"></a> * @type {boolean}
<a name="line107"></a> * @private
<a name="line108"></a> */
<a name="line109"></a>goog.net.XhrMonitor_.prototype.enabled_ = goog.userAgent.GECKO;
<a name="line110"></a>
<a name="line111"></a>
<a name="line112"></a>/**
<a name="line113"></a> * Set the enabled flag.
<a name="line114"></a> * @param {boolean} val The new value.
<a name="line115"></a> */
<a name="line116"></a>goog.net.XhrMonitor_.prototype.setEnabled = function(val) {
<a name="line117"></a>  this.enabled_ = goog.userAgent.GECKO &amp;&amp; val;
<a name="line118"></a>};
<a name="line119"></a>
<a name="line120"></a>
<a name="line121"></a>/**
<a name="line122"></a> * Pushes a new context onto the stack.
<a name="line123"></a> * @param {Object|string} context An object or string indicating the source of
<a name="line124"></a> *     the execution context.
<a name="line125"></a> */
<a name="line126"></a>goog.net.XhrMonitor_.prototype.pushContext = function(context) {
<a name="line127"></a>  if (!this.enabled_) return;
<a name="line128"></a>
<a name="line129"></a>  var key = goog.net.XhrMonitor_.getKey(context);
<a name="line130"></a>  this.logger_.finest(&#39;Pushing context: &#39; + context + &#39; (&#39; + key + &#39;)&#39;);
<a name="line131"></a>  this.stack_.push(key);
<a name="line132"></a>};
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>/**
<a name="line136"></a> * Pops the most recent context off the stack.
<a name="line137"></a> */
<a name="line138"></a>goog.net.XhrMonitor_.prototype.popContext = function() {
<a name="line139"></a>  if (!this.enabled_) return;
<a name="line140"></a>
<a name="line141"></a>  var context = this.stack_.pop();
<a name="line142"></a>  this.logger_.finest(&#39;Popping context: &#39; + context);
<a name="line143"></a>  this.updateDependentContexts_(context);
<a name="line144"></a>};
<a name="line145"></a>
<a name="line146"></a>
<a name="line147"></a>/**
<a name="line148"></a> * Checks to see if there are any outstanding XmlHttpRequests that were
<a name="line149"></a> * started in the given context.
<a name="line150"></a> * @param {Object|string} context An object or string indicating the execution
<a name="line151"></a> *     context to check.
<a name="line152"></a> * @return {boolean} Whether there are any outstanding requests linked to the
<a name="line153"></a> *     context.
<a name="line154"></a> */
<a name="line155"></a>goog.net.XhrMonitor_.prototype.isContextSafe = function(context) {
<a name="line156"></a>  if (!this.enabled_) return true;
<a name="line157"></a>
<a name="line158"></a>  var deps = this.contextsToXhr_[goog.net.XhrMonitor_.getKey(context)];
<a name="line159"></a>  this.logger_.fine(&#39;Context is safe : &#39; + context + &#39; - &#39; + deps);
<a name="line160"></a>  return !deps;
<a name="line161"></a>};
<a name="line162"></a>
<a name="line163"></a>
<a name="line164"></a>/**
<a name="line165"></a> * Marks an XHR object as being open.
<a name="line166"></a> * @param {Object} xhr An XmlHttpRequest object that is about to be opened.
<a name="line167"></a> */
<a name="line168"></a>goog.net.XhrMonitor_.prototype.markXhrOpen = function(xhr) {
<a name="line169"></a>  if (!this.enabled_) return;
<a name="line170"></a>
<a name="line171"></a>  var hc = goog.getHashCode(xhr);
<a name="line172"></a>  this.logger_.fine(&#39;Opening XHR : &#39; + hc);
<a name="line173"></a>
<a name="line174"></a>  // Update all contexts that are currently on the stack.
<a name="line175"></a>  for (var i = 0; i &lt; this.stack_.length; i++) {
<a name="line176"></a>    var context = this.stack_[i];
<a name="line177"></a>    this.addToMap_(this.contextsToXhr_, context, hc);
<a name="line178"></a>    this.addToMap_(this.xhrToContexts_, hc, context);
<a name="line179"></a>  }
<a name="line180"></a>};
<a name="line181"></a>
<a name="line182"></a>
<a name="line183"></a>/**
<a name="line184"></a> * Marks an XHR object as being closed.
<a name="line185"></a> * @param {Object} xhr An XmlHttpRequest object whose request has completed.
<a name="line186"></a> */
<a name="line187"></a>goog.net.XhrMonitor_.prototype.markXhrClosed = function(xhr) {
<a name="line188"></a>  if (!this.enabled_) return;
<a name="line189"></a>
<a name="line190"></a>  var hc = goog.getHashCode(xhr);
<a name="line191"></a>  this.logger_.fine(&#39;Closing XHR : &#39; + hc);
<a name="line192"></a>
<a name="line193"></a>  // Delete the XHR look up and remove the XHR from any contexts.
<a name="line194"></a>  delete this.xhrToContexts_[hc];
<a name="line195"></a>  for (var context in this.contextsToXhr_) {
<a name="line196"></a>    goog.array.remove(this.contextsToXhr_[context], hc);
<a name="line197"></a>    if (this.contextsToXhr_[context].length == 0) {
<a name="line198"></a>      delete this.contextsToXhr_[context];
<a name="line199"></a>    }
<a name="line200"></a>  }
<a name="line201"></a>};
<a name="line202"></a>
<a name="line203"></a>
<a name="line204"></a>/**
<a name="line205"></a> * Updates any contexts that were dependent on the given XHR request with any
<a name="line206"></a> * XHRs that were opened by the same XHR.  This is used to track Iframes that
<a name="line207"></a> * open XHRs which then in turn open an XHR.
<a name="line208"></a> * @param {string} xhrHc The hashcode for the XHR to update.
<a name="line209"></a> * @private
<a name="line210"></a> */
<a name="line211"></a>goog.net.XhrMonitor_.prototype.updateDependentContexts_ = function(xhrHc) {
<a name="line212"></a>  // Update any contexts that are dependent on this XHR with any requests
<a name="line213"></a>  // registered with the XHR as a base context.  This is used for the situation
<a name="line214"></a>  // when an XHR event triggers another XHR.  The original XHR is closed, but
<a name="line215"></a>  // the source context needs to be informed about any XHRs that were opened as
<a name="line216"></a>  // a result of the first.
<a name="line217"></a>  var contexts = this.xhrToContexts_[xhrHc];
<a name="line218"></a>  var xhrs = this.contextsToXhr_[xhrHc];
<a name="line219"></a>  if (contexts &amp;&amp; xhrs) {
<a name="line220"></a>    this.logger_.finest(&#39;Updating dependent contexts&#39;);
<a name="line221"></a>    goog.array.forEach(contexts, function(context) {
<a name="line222"></a>      goog.array.forEach(xhrs, function(xhr) {
<a name="line223"></a>        this.addToMap_(this.contextsToXhr_, context, xhr);
<a name="line224"></a>        this.addToMap_(this.xhrToContexts_, xhr, context);
<a name="line225"></a>      }, this);
<a name="line226"></a>    }, this);
<a name="line227"></a>  }
<a name="line228"></a>};
<a name="line229"></a>
<a name="line230"></a>
<a name="line231"></a>/**
<a name="line232"></a> * Adds a value to a map of arrays.  If an array hasn&#39;t been created for the
<a name="line233"></a> * provided key, then one is created.
<a name="line234"></a> * @param {Object} map The map to add to.
<a name="line235"></a> * @param {string|number} key the key.
<a name="line236"></a> * @param {string|number} value The value.
<a name="line237"></a> * @private
<a name="line238"></a> */
<a name="line239"></a>goog.net.XhrMonitor_.prototype.addToMap_ = function(map, key, value) {
<a name="line240"></a>  if (!map[key]) {
<a name="line241"></a>    map[key] = [];
<a name="line242"></a>  }
<a name="line243"></a>  if (!goog.array.contains(map[key], value)) {
<a name="line244"></a>    map[key].push(value);
<a name="line245"></a>  }
<a name="line246"></a>};
<a name="line247"></a>
<a name="line248"></a>
<a name="line249"></a>/**
<a name="line250"></a> * Singleton XhrMonitor object
<a name="line251"></a> * @type {goog.net.XhrMonitor_}
<a name="line252"></a> */
<a name="line253"></a>goog.net.xhrMonitor = new goog.net.XhrMonitor_();
</pre>


</body>
</html>
